package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/*
    用来处理与员工有关的一切请求
 */
@RequestMapping("/employee")
@RestController
public class EmployeeController {
    
    //注入service
    @Autowired
    private EmployeeService es;
    
    
    /**
     * 更新员工 （包括更新员工的状态和员工的信息）
     * @param employee
     * @return
     */
    @PutMapping
    public R update(@RequestBody Employee employee , HttpSession session){
    
        System.out.println("=====================Controller的ID：：：" + Thread.currentThread().getId());
        
        //1. 调用service
        Employee e = (Employee) session.getAttribute("employee");
        int row = es.update(employee , e.getId());
        
        //2. 判断结果
        if(row >0 ){
            return R.success("更新状态成功");
        }else{
            return R.error("更新状态失败");
        }
    }
    
    /**
     * 分页查询员工信息
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R<IPage<Employee>> findPage(PageParam pageParam){
    
        try {
            //1. 调用service
            IPage<Employee> page = es.findPage(pageParam);
        
            //2. 返回数据
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败！");
        }
    
    }
    
    
    /**
     * 添加员工  --- localhost:8080/employee
     * @param employee
     * @return
     */
    @PostMapping
    public R add(@RequestBody Employee employee , HttpSession session){
        Employee loginEmployee = (Employee) session.getAttribute("employee");
        //1. 调用service
        int row = es.add(employee ,  loginEmployee.getId());
        
        //2. 判定
        if(row >0 ){
            return R.success("员工添加成功");
        }else{
            return R.error("员工添加失败");
        }
        
    }
    
    /**
     * 退出登录
     *     1. 在服务器端要把曾经登录成功之后保存的员工对象给删除了。
     *     2. 还可以更进一步，就是把session都销毁了。
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        try {
            //1. 清空session即可
            //session.removeAttribute("employee");
        
            //让session失效，把session的数据全部干掉，并且回收这个内存。
            session.invalidate();
        
            return R.success("退出成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("退出失败");
        }
    }
    
    /**
     * 登录
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee , HttpSession session){
    
        //1. 交代service干活
        Employee loginEmployee = es.login(employee);
        
        //2. 判断结果：
        if(loginEmployee == null){ //登录失败！
            return R.error("用户名或者密码错误！");
        }else { //登录成功！
    
            //1. 先把员工对象保存到session作用域
            session.setAttribute("employee", loginEmployee);
    
            //2. 返回信息给页面
            return R.success(loginEmployee);
        }
    }
}
